package ru.ifmo.tanya.db;

import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import ru.ifmo.tanya.core.huntgroups.HuntGroup;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;


public class HuntGroupManager {

    private ParameterizedRowMapper<HuntGroup> mapper = new ParameterizedRowMapper<HuntGroup>() {
        @Override
        public HuntGroup mapRow(ResultSet resultSet, int i) throws SQLException {
            return new HuntGroup(resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("extension"));
        }
    };

    public HuntGroup getHuntGroupByName(String name) {
        List<HuntGroup> groups = DataBaseManager.getJdbcTemplate().query("SELECT * FROM hunt_groups WHERE name = ?", mapper, name);
        if (!groups.isEmpty()) {
            return groups.get(0);
        }
        return null;
    }

    public HuntGroup getHuntGroupById(int id) {
        List<HuntGroup> groups = DataBaseManager.getJdbcTemplate().query("SELECT * FROM hunt_groups WHERE id = ?", mapper, id);
        if (!groups.isEmpty()) {
            return groups.get(0);
        }
        return null;
    }

    public List<HuntGroup> getAllHuntGroups() {
        return DataBaseManager.getJdbcTemplate().query("SELECT * FROM hunt_groups", mapper);
    }
}
